<template>
  <div class="app-container">
    <el-table :data="row" border style="width: 100%">
      <el-table-column type="index" label="序号" width="50"> </el-table-column>
      <el-table-column prop="title" label="课程名称" width="180">
      </el-table-column>
      <el-table-column prop="teacherName" label="讲师名称" width="180">
      </el-table-column>
      <el-table-column prop="price" label="课程价格" width="180">
      </el-table-column>
      <el-table-column prop="lessonNum" label="总课时数" width="180">
      </el-table-column>
      <el-table-column prop="buyCount" label="销售数" width="180">
      </el-table-column>
      <el-table-column prop="viewCount" label="浏览量" width="180">
      </el-table-column>
      <el-table-column prop="status" label="状态" width="180">
      </el-table-column>
      <el-table-column prop="gmtCreated" label="创建时间" width="180">
      </el-table-column>
      <el-table-column prop="gmtModified" label="更新时间" width="180">
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="danger" @click="deleteCourse(scope.row.courseId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="page"
      background
      layout="total,prev, pager, next"
      :page-size="limit"
      :current-page="currentPage"
      :total="total"
      @current-change="fetchData"
    >
    </el-pagination>
  </div>
</template>


<script>
import courseApi from "@/api/course";
export default {
  data() {
    return {
      limit: 2,
      currentPage: 1,
      total: 0,
      row: [],
    };
  },
  created() {
    this.showCourseInfo();
  },
  methods: {
    //给个默认值为1
    fetchData(page = 1) {
      //你们不用思考如何刷新页面 因为 data中的值发送改变了
      //他自动刷新页面
      //用户点击了page以后 page的值可能是1 可能是2 可能是3
      this.currentPage = page;
      courseApi
        .findCourseByPage(this.currentPage, this.limit)
        .then((res) => {
          this.row = res.data.row;
          this.total = res.data.total;
        })
        .catch((err) => {});
    },
    showCourseInfo() {
      courseApi
        .findCourseByPage(this.currentPage, this.limit)
        .then((res) => {
          this.row = res.data.row;
          this.total = res.data.total;
        })
        .catch((err) => {});
    },
    deleteCourse(id) {
      this.$confirm("是否要删除该课程信息", "删除课程", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        courseApi
          .deleteCourseById(id)
          .then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.showCourseInfo();
          })
          .catch((err) => {
            this.$message({
              type: "error",
              message: "删除失败!",
            });
          });
      });
    },
  },
};
</script>

<style >
</style>